package com.zzl.car.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.CarBaseInfo;
import com.zzl.car.entity.CarDetailInfo;
import com.zzl.car.entity.CarOrder;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.ZzwCarBaseInfoMapper;
import com.zzl.car.mapper.ZzwCarDetailInfoMapper;
import com.zzl.car.util.OssUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Service
public class ZzwCarBaseInfoService {
    @Autowired
    private ZzwCarBaseInfoMapper carBaseInfoMapper;
    @Autowired
    private ZzwCarDetailInfoService carDetailInfoService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OssUtil ossUtil;

    /**
     * 添加车辆信息
     * @param map 不包含图片信息
     * @param image 图片信息
     * @return
     */
    public String addCarBaseInfo (Map<String,Object> map,
                                  MultipartFile image) throws IOException {

        // 根据 品牌名 和 具体型号 来判断是否重复
        List<CarBaseInfo> carBaseInfos = getAllCarBaseInfo();

        for(CarBaseInfo c : carBaseInfos) {
            if(c.getBrand().equals(map.get("brand")) && c.getDetailType().equals(map.get("detailType")))
                // 有相同信息，抛出异常
                throw new ZdpException(2910,"添加失败，存在相同 品牌名 和 具体型号 的车辆信息");
        }

        // 上传图片到 OSS
        // 图片前缀
        String prefix = "CarBaseInfo";
        // 返回 上传到服务器的图片路径
        String imageUrl = ossUtil.uploadFile(image,prefix);

        //图片ID 使用系统时间（我就不信这样能重复）
        int id = Integer.valueOf(String.valueOf(System.currentTimeMillis()).substring(1,9));
        System.out.println(id);

        CarBaseInfo car = new CarBaseInfo();
        car.setId(id);
        car.setBrand(map.get("brand").toString());
        car.setDetailType(map.get("detailType").toString());
        car.setSeatAmount(Integer.valueOf(map.get("seatAmount").toString()));
        car.setLevel(map.get("level").toString());
        car.setImage(imageUrl);

        if(carBaseInfoMapper.addCarBaseInfo(car) > 0)
            return "1";
        else
            return "0";
    }

    /**
     * 修改车辆基础信息
     * @param map
     * @param image
     * @return
     */
    public String modifyCarBaseInfo(Map<String,Object> map,
                                    MultipartFile image) throws IOException{
        String imageUrl = null;
        if(image != null) {
            // 上传图片到 OSS
            // 图片前缀
            String prefix = "CarBaseInfo";
            // 返回 上传到服务器的图片路径
            imageUrl = ossUtil.uploadFile(image, prefix);
            System.out.println(image.getOriginalFilename());
        }

        CarBaseInfo car = new CarBaseInfo();
        car.setId(Integer.valueOf(map.get("id").toString()));
        car.setBrand(map.get("brand").toString());
        car.setDetailType(map.get("detailType").toString());
        car.setSeatAmount(Integer.valueOf(map.get("seatAmount").toString()));
        car.setLevel(map.get("level").toString());
        car.setImage(imageUrl);

        if(carBaseInfoMapper.modifyCarBaseInfo(car) > 0)
            return "1";
        else
            return "0";
    }


    /**
     * 得到全部车辆的基础信息 --> 分页功能
     * @param currentPage 当前页面 页码
     * @param pageAmount  每页项数
     * @return 返回分页后的信息
     */
    public PageInfo<CarBaseInfo> getAllCarBaseInfoPage(int currentPage,int pageAmount){
        PageInfo<CarBaseInfo> pageInfo = null;
        List<CarBaseInfo> carBaseInfos = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        carBaseInfos = carBaseInfoMapper.getAllCarBaseInfo();

        pageInfo = new PageInfo<>(carBaseInfos);
        pageInfo.setList(carBaseInfos);

        return pageInfo;
    }

    // 得到全部车辆的基础信息
    public List<CarBaseInfo> getAllCarBaseInfo(){
        return carBaseInfoMapper.getAllCarBaseInfo();
    }

    // 删除车辆基础信息
    public String deleteCarBaseInfo(Integer id){
        List<CarDetailInfo> list = carDetailInfoService.getAllCarDetailInfo();
        for (CarDetailInfo l:list) {
            if (l.getCarBaseInfo().getId().equals(id)) {
                throw new ZdpException(2920, "车辆详细信息中存在该车辆基础信息，不可删除");
            }
        }

        int result = carBaseInfoMapper.deleteCarBaseInfo(id);
        if(result > 0)
            return "1";
        else
            throw new ZdpException(2921,"删除失败");
    }

    // 条件模糊查询 brand detailType
    public PageInfo<CarBaseInfo> searchCarBaseInfo(String brand,String detailType,int currentPage){

        int pageAmount = 5;
        PageInfo<CarBaseInfo> pageInfo = null;
        List<CarBaseInfo> carBaseInfos = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        carBaseInfos = carBaseInfoMapper.searchCarBaseInfo(brand,detailType);

        pageInfo = new PageInfo<>(carBaseInfos);
        pageInfo.setList(carBaseInfos);

        return pageInfo;
    }
}
